package cn.bupt.adt.service.impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bupt.adt.dao.autogenerate.CheckInstiAppUserMapper;
import cn.bupt.adt.dao.autogenerate.CheckInstiViewMapper;
import cn.bupt.adt.dao.autogenerate.InspectOrderViewMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionAppUserMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionViewMapper;
import cn.bupt.adt.entity.autogenerate.CheckInstiAppUser;
import cn.bupt.adt.entity.autogenerate.CheckInstiView;
import cn.bupt.adt.entity.autogenerate.CheckInstiViewExample;
import cn.bupt.adt.entity.autogenerate.InspectOrderView;
import cn.bupt.adt.entity.autogenerate.InstitutionAppUser;
import cn.bupt.adt.entity.autogenerate.InstitutionView;
import cn.bupt.adt.entity.autogenerate.InstitutionViewExample;
import cn.bupt.adt.entity.autogenerate.Order;
import cn.bupt.adt.service.ICheckInstiAppUserService;
import cn.bupt.adt.service.IInstitutionAppUserService;
import cn.bupt.adt.util.MD5Util;
	@Service
	public class CheckInstiAppUserServiceImpl  implements ICheckInstiAppUserService {

		@Autowired
		CheckInstiViewMapper checkInstiViewMapper;
		
		@Autowired
		InspectOrderViewMapper inspectOrderViewMapper;
		
		@Autowired
		CheckInstiAppUserMapper checkInstiAppUserMapper;
		
		@Override
		public CheckInstiView getInsByAccount(String account) {
			CheckInstiViewExample ex = new CheckInstiViewExample();
			ex.createCriteria().andAccountEqualTo(account);
			List<CheckInstiView> lists = checkInstiViewMapper.selectByExample(ex);
			return lists.size() == 1?lists.get(0):null;
		}

		@Override
		public void updateLoginTimeById(Integer id, Date loginTime) {
			CheckInstiAppUser user = checkInstiAppUserMapper.selectByPrimaryKey(id);
			if(user != null) {
				user.setLastLoginTime(loginTime);
				checkInstiAppUserMapper.updateByPrimaryKeySelective(user);
			}
		}

		@Override
		public CheckInstiAppUser getInsById(Integer userId) {
			return checkInstiAppUserMapper.selectByPrimaryKey(userId);
		}

		@Override
		public boolean changePasswd(CheckInstiAppUser ins, String newPasswd) {
			ins.setPasswd(MD5Util.MD5(newPasswd));
			return checkInstiAppUserMapper.updateByPrimaryKeySelective(ins) == 1;
		}

		@Override
		public void updateInsAppUser(CheckInstiAppUser ins) {
			checkInstiAppUserMapper.updateByPrimaryKeySelective(ins);
		}

		@Override
		public CheckInstiView getInsViewById(Integer userId) {
			CheckInstiViewExample example = new CheckInstiViewExample();
			example.createCriteria().andIdEqualTo(userId);
			List<CheckInstiView> list = checkInstiViewMapper.selectByExample(example);
			return list.size() == 1?list.get(0):null;
		}

		
	}
